/**
 * 
 */
package com.speaktoitcookbook.dao.impl;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.List;

import com.speaktoitcookbook.bean.ThirdAccount;
import com.speaktoitcookbook.bean.UserInfo;
import com.speaktoitcookbook.dao.IThirdAccountDao;
import com.speaktoitcookbook.util.DBUtil;
import com.speaktoitcookbook.util.TransformUtil;

/**
 * @author 赛艳志
 * 
 */
public class ThirdAccountDaoImpl implements IThirdAccountDao {
    private Connection conn = null;

    public ThirdAccountDaoImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public boolean doCreate(ThirdAccount thirdAccount) {
        boolean result = false;
        String sql = "insert into `tb_thirdaccount`(`userID`, `accId`, `acType`) values(?, ?, ?)";
        Object[] params = TransformUtil.beanTOArray(thirdAccount);
        result = DBUtil.insert(conn, sql, params);
        return result;
    }

    @Override
    public boolean doDelete(ThirdAccount thirdAccount) {
        boolean result = false;
        String sql = "delete from `tb_thirdaccount` where `userID` = ? and `accId` = ? and `acType` = ?";
        Object[] params = TransformUtil.beanTOArray(thirdAccount);
        result = DBUtil.delete(conn, sql, params);
        return result;
    }

    @Override
    public boolean doUpdate(ThirdAccount thirdAccount) {
        boolean result = false;
        String sql = "update `tb_thirdaccount` set `accId` = ? where `userID` = ? and `acType` = ?";

        int iterm = 0;
        Object[] params = new Object[3];
        params[iterm++] = thirdAccount.getThirdAccountID();
        params[iterm++] = thirdAccount.getUserID();
        params[iterm++] = thirdAccount.getType();

        result = DBUtil.update(conn, sql, params);
        return result;
    }

    @Override
    public List<ThirdAccount> getThirdAccount(String userID) {
        List<ThirdAccount> result = null;
        String sql = "select * from `tb_thirdaccount` where `userID` = ?";

        List<Object[]> all = DBUtil.query(conn, sql, userID);
        if (all.size() > 0) {
            result = new ArrayList<ThirdAccount>();
            for (Object[] objects : all) {
                ThirdAccount thirdaccont = new ThirdAccount();
                thirdaccont = (ThirdAccount) TransformUtil.arrayToBean(objects,
                        ThirdAccount.class);
                result.add(thirdaccont);
            }
        }
        return result;
    }

    @Override
    public List<UserInfo> getThirdAccount(Object... params) {
        List<UserInfo> result = null;
        StringBuffer sql = new StringBuffer(
                "select `tb_userinfo`.* from `tb_userinfo`, `tb_thirdaccount` ");
        if (null != params && params.length > 0) {
            result = new ArrayList<UserInfo>();
            for (Object param : params) {
                sql.append(param);
            }
            List<Object[]> all = DBUtil.query(conn, sql.toString());

            for (Object[] objects : all) {
                UserInfo userinfo = (UserInfo) TransformUtil.arrayToBean(
                        objects, UserInfo.class);
                result.add(userinfo);
            }
        }
        return result;
    }

}
